Migrating Your Flash Applications to Macromedia Central
Table of Contents
Testing and Debugging Your Application
Using trace to debug doesn't get you very far when developing
Central applications. Presumably when you use any of Central's functionality
your application won't behave as intended within the standard Flash Player
or IDE. Instead, it's recommended that you use a method of testing that incorporates
a debugging application (client) while your application serves data to the
client over LocalConnection. Thankfully Macromedia put together an application
and API that helps with this testing process (see Tracer in the Application
Finder). In addition, Macromedia included a Debug panel with the SDK that allows
the implementation of the trace function. You can find the MXP
installer and source code in the SDK\Utilities\DebugPanel folder. After you
install it, you can simply send information to the Debug panel by using the
trace function:
trace("Hello, this is from Central");
The information is then displayed within the Debug panel. Built-in support for tracing and formatting data types such as arrays and XML will also be available. The Debug panel uses the same LocalConnection client/server method I mentioned just above.
Enabling debugging of your application is a simple process:
debug.enabled = true;
This allows Central-level annotations to be posted to the Debug panel. An example might include the amount of time it takes to parse a WSDL document. The standard debugger built into the Flash IDE will not be operational, however.
Initiating Your Application
You have your application all set up and you're just waiting to get it into Central. The first couple of steps include initiating your application so that Central knows it's indeed a Central application.
Central.initApplication
Although it will more than likely be your last line of code, the first step
in initiating your application is calling the initApplication method.
The Central.initApplication method tells the Central shell that
your movie has loaded. This method accepts two parameters: the application
object and the event handler object. The application object references the "root" timeline
of your application while the event handler object is the location of your
application-specific event handlers (more information on those just ahead).
You'll more than likely put your initiation code on the root timeline, in which
case the following example is all you need to get things started:
Central.initApplication(this, this);
Simple, right? One issue may arise when you use screens or forms in Flash MX Professional 2004. For example, if you place your code on the topmost screen, the root of your application is now the parent movie clip, "root," which holds your screen and form components. In this case, a simple modification to the code tells Central how everything is set up:
Central.initApplication(_parent, this);
Application.onActivate
The Central shell calls this function when the application is about to be
displayed in the application window. Within the onActivate() function
you define your initial variables, make connections to remote data, and handle
any application initialization. Without this step, most of the additional functionalities
of Central are lost.
When Central calls onActivate() it passes five parameters to
it. The first one is the Shell object reference which allows you to make calls
to the Central shell methods.
In the following example, onActivate() assigns the shell callback
object to the variable gShell and then calls the shell's setStatus() function:
function onActivate(shellRef, appID, shellID, baseTabIndex, initialData)
{
gShell = shellRef;
gShell.setStatus("The onActivate call has been received");
}
appID
The appID parameter provides a unique ID number for the
current instance of your application. By incorporating this ID into the
name of a local shared object, you can use it to create local shared objects
that are specific to the application instance. In these local shared objects
you can store information such as the display state. You can use the same
technique to create uniquely named local connection objects for communicating
with pods or agents associated with your application.
shellD
The shellID parameter provides a unique ID number for the
shell (application window) that the application runs in. By combining
the shellID parameter with the appID parameter
in a variable name, you can allow multiple instances of the same application
to create per-instance local connections. Use these local connections
to communicate among applications, pods, and agents. An application should
not use the shellID parameter to create persistent local
shared objects because the value changes across sessions.
baseTabIndex
The baseTabIndex parameter provides seamless tab-navigation
support. Set the tabbing order indexes on your application controls to
start at this base index so there won't be any conflict with surrounding
application window controls.
initialData
You can use the initialData parameter to pass application-specific
data to an application from a pod. This can be useful when a pod calls Console.loadApplication() to
display the application in the shell. For example, the application might
initially display data contained in the appData parameter.
The appData parameter can be a string or object reference.
The following onActivate() function is from the StockWatcher
application's stock.as file. It assigns the shell callback object to the
variable gShell, assigns the application ID to the variable gAppID,
and creates a unique string identifying the application by concatenating
the value of gAppName and shellID:
function onActivate(shellRef, appID, shellID, baseTabIndex, initialData)
{
gShell = shell;
gAppID = id;
gAppName += shellID;
textField.tabIndex = baseTabIndex + 1;
dataGrid.tabIndex = baseTabIndex + 2;
// additional initialization
}
Application.onResize
In addition to creating your shell references, setting up the tab order,
and so on within the onActivate() function, this is where
you would make any connections to local shared objects and initiate any
LocalConnections to your application's agent. It is also recommended that
you call the onResize event handler at this point to make
sure your application appears correctly within the application shell.
The onResize() function is called by the Central shell when
the application window is resized. In this function, your application
should call shellRef.getBounds() to determine the new window
size and adjust its own size to fill the window accordingly. The getBounds() method
returns an object with two properties: height and width.
Application.getMinimumSize, Application.getMaximumSize
Each time your application is displayed in the application window, the
Central shell calls getMinimumSize() and getMaximumSize() to
determine the minimum and maximum dimensions for your application. Each
function should return an object containing two properties representing
the minimum and maximum width and height of your application. If a user
switches to your application while the application window is smaller than
your specified minimum size, the Central application window expands accordingly.
Similarly, if the user switches to the application while the application
window is larger than the application's maximum size, Central resizes
the application window to the specified size:
function getMinimumSize()
{
return {width:400, height:400};
}
function getMaximumSize()
{
return {width:800, height:600};
}
Each of these functions is optional. If your application does not include either one, the application window will not automatically resize itself when Central makes the call.
Application.showPrefercnces
The showPreferences() function is called by the Central
shell when the user chooses Preferences from the ApplicationName menu
item. In this function, you are encouraged to use ActionScript calls that
display a Preferences screen for your application. The preferences option
for an application only appears if the following attribute is in the application
tag of the Product XML Descriptor File:
hasPreferences="true"
Otherwise, it does not appear. Local shared objects are a good way to store application-specific preferences data that must persist between application sessions.
Cleaning Up Your Application
When someone switches from your application or if your application is uninstalled, it is best to clean up any processes running as well as any files left or anything else not cleaned up by the uninstallation process.
Application.onDeactivate
The Central shell calls the onDeactivate() function when
the application is about to be shut down. This can happen when the user
selects another application to be displayed in the application window,
the application window is closed, or the user quits Central. You should
assume that your application is going to be shut down after this method
is called. The onDeactivate() function is a good place to
save data, remove setInterval statements, close data connections,
and clear global variables. Be aware that you cannot use local connection
methods in the onDeactivate() function.
Within this function, you should implement some sort of functionality that remembers users' last state when they used your application. This conveys the feeling of "tabbed browsing," which allows users to switch to another application and then switch back to yours and continue right where they left off. Consider, for example, a text editor that automatically saves any text in a field when a user switches to a news application. The user copies a line of text from the news application, switches back to the text editor, and pastes the text into what they're working on.
Application.onUninstall
The Central shell calls the onUninstall() function when
the application is being uninstalled. This occurs when the user selects
View > My Applications and then uninstalls the application in the My
Applications screen. Be sure to Uinclude code in this function that cleans
up any local shared objects created by your application by setting them
to zero. Central does not do this for you. However, Central does delete
files cached with the addToLocalInternetCache() command when
the application caching them is uninstalled.